﻿using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity;
using CRM.Extensions;
using CRM.Enums;
using CRM.Models.SocialMarketing;
using HotChocolate;
using Newtonsoft.Json;

namespace CRM.Models
{
    [Table("user")]
    [Comment("用户管理_用户")]
    [Index(nameof(UserName), Name = "UserName")]
    [Index(nameof(Mobile), Name = "Mobile")]
    [Index(nameof(RuleId), Name = "RuleId")]
    [Index(nameof(GroupId), Name = "GroupId")]
    [Index(nameof(Pid), Name = "Pid")]
    [Index(nameof(Type), Name = "Type")]
    [Index(nameof(State), Name = "State")]
    [Index(nameof(CompanyId), Name = "CompanyId")]
    [Index(nameof(OemId), Name = "OemId")]
    [Index(nameof(Sort), Name = "Sort")]
    [Index(nameof(HashCode), Name = "HashCode")]
    public class User : IdentityUser<int>, IHasTimestamps
    {
        //[Column("id", TypeName = "int(11)"), Key, Required]
        //public override int Id { get; set; }

        //[Column("username", TypeName = "varchar(255)"), Comment("用户名(唯一)"), Required]
        //public override string UserName { get; set; }

        [Column("truename", TypeName = "varchar(255)"), Comment("姓名")]
        public string TrueName { get; set; } = string.Empty;

        public override string NormalizedUserName => UserName;

        [NotMapped]
        public string DisplayName
        {
            get => TrueName;
            set => TrueName = value;
        }

        [NotMapped, GraphQLIgnore] public string Password { get; set; } = string.Empty;
        [GraphQLIgnore]public override string PasswordHash { get; set; }

        [Column("attestation", TypeName = "tinyint"), Comment("手机号码认证 1未认证2已认证")]
        public Attestations Attestation { get; set; } = Attestations.DISABLE;

        public enum Attestations
        {
            None,
            DISABLE,
            ENABLE
        }

        [Column("mobile", TypeName = "varchar(20)"), Comment("手机")]
        public string Mobile { get; set; } = string.Empty;

        [Column("email", TypeName = "varchar(255)"), Comment("邮箱")]
        public override string Email { get; set; } = string.Empty;

        [Column("rule_id", TypeName = "int(11)"), Comment("权限组ID")]
        public int RuleId { get; set; } = 0;

        [Column("group_id", TypeName = "int(11)"), Comment("用户组ID")]
        public int GroupId { get; set; } = 0;

        [Column("type", TypeName = "tinyint"), Comment("用户类型1超管2管理员3公司管理员4员工")]
        public Types Type { get; set; } = Types.Admin;
        [GraphQLName("UserTypes")]
        public enum Types
        {
            None,
            Super,
            Admin,
            CompanyAdmin,
            Employee
        }

        [Column("pid", TypeName = "int(11)"), Comment("父级用户ID")]
        public int Pid { get; set; }

        [Column("pids", TypeName = "varchar(255)"), Comment("创建用户过程")]
        public string Pids { get; set; } = string.Empty;

        [Column("state", TypeName = "tinyint"), Comment("状态1启用2禁用")]
        public States State { get; set; } = States.ENABLE;

        public bool Enabled
        {
            get => State == States.ENABLE;
            set => State = value ? States.ENABLE : States.DISABLE;
        }

        [Column("level", TypeName = "int(11)"), Comment("层级深度")]
        public int Level { get; set; }

        [Column("avatar", TypeName = "varchar(255)"), Comment("头像")]
        public string Avatar { get; set; } = string.Empty;

        [Column("sort", TypeName = "int(11)"), Comment("排序字段")]
        public long Sort { get; set; }

        [Column("hashCode", TypeName = "bigint(20)"), Comment("用户名 hashCode")]
        public long HashCode { get; set; }

        [Column("company_id", TypeName = "int(11)"), Comment("公司ID")]
        public int CompanyId { get; set; }

        public Company Company { get; set; }

        [Column("oem_id", TypeName = "int(11)"), Comment("贴牌OEM ID(分区id)")]
        public int OemId { get; set; } = -1;

        public Oem Oem { get; set; }

        public SocialAccount SocialAccount { get; set; }

        [Column("first_login_web", TypeName = "Timestamp"), Comment("首次WEB登录")]
        public DateTime? FirstLoginWeb { get; set; } = null;

        [Column("first_login_exe", TypeName = "Timestamp"), Comment("首次EXE登录")]
        public DateTime? FirstLoginExe { get; set; } = null;

        [Column("login_time", TypeName = "Timestamp"), Comment("登录时间")]
        public DateTime? LoginTime { get; set; } = null;
        
        [Column(nameof(Validity), TypeName = "Timestamp"), Comment("员工账户有效期"), 
         DisplayFormat(DataFormatString = "yyyy-MM-DD", ConvertEmptyStringToNull = true)]
        public DateTime? Validity { get; set; }

        [Column("created_at", TypeName = "Timestamp")]
        public DateTime CreatedAt { get; set; }

        [Column("updated_at", TypeName = "Timestamp")]
        public DateTime UpdatedAt { get; set; }

        /// <summary>
        /// 用户配置
        /// </summary>
        public string? Settings { get; set; }

        [NotMapped]
        public UserSettings SettingsObj
        {
            get => JsonConvert.DeserializeObject<UserSettings>(!string.IsNullOrWhiteSpace(Settings) ? Settings : "{}");
            set => Settings = JsonConvert.SerializeObject(value);
        }

        /// <summary>
        /// 是否已授权vk
        /// </summary>
        public bool HasOauthVk => !string.IsNullOrEmpty(SocialAccount?.VkToken);

        [MaxLength(36)]
        public string LoginStamp { get; set; }
    }

    /// <summary>
    /// 用户配置
    /// </summary>
    public class UserSettings
    {
        /// <summary>
        /// 导出客户邮箱前验证邮箱有效性
        /// </summary>
        public bool ValidateEmailBeforeExport { get; set; }

        /// <summary>
        /// 仅导出有效邮箱的客户
        /// </summary>
        public bool OnlyExportValidEmails { get; set; }
    }
}